package com.smart.community.region.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.feign.region.dto.AddressInfoDTO;
import com.smart.community.feign.region.dto.CommunityInfoDTO;
import com.smart.community.region.service.IAddressService;
import com.smart.community.region.service.ICommunityService;
import com.smart.community.region.vo.CommunityVO;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 区域地址Feign接口实现Controller 提供根据房户ID、单元ID、楼栋ID、社区ID获取详细地址的功能
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Slf4j
@RestController
@RequestMapping("/feign/region")
public class RegionAddressFeignController {

	@Autowired
	private IAddressService addressService;

	@Autowired
	private ICommunityService communityService;

	/**
	 * 根据房户ID获取详细地址
	 * 
	 * @param householdId 房户ID
	 * @return 详细地址信息
	 * @throws Exception
	 */
	@GetMapping("/region/address/household/{householdId}")
	public AddressInfoDTO getAddressByHouseholdId(@PathVariable("householdId") Long householdId) throws Exception {
		AddressInfoDTO result = addressService.getAddressByHouseholdId(householdId);
		log.info("根据房户ID获取详细地址成功，房户ID：{}", householdId);
		return result;
	}

	/**
	 * 根据单元ID获取详细地址
	 * 
	 * @param unitId 单元ID
	 * @return 详细地址信息
	 * @throws Exception
	 */
	@GetMapping("/region/address/unit/{unitId}")
	public AddressInfoDTO getAddressByUnitId(@PathVariable("unitId") Long unitId) throws Exception {
		AddressInfoDTO result = addressService.getAddressByUnitId(unitId);
		log.info("根据单元ID获取详细地址成功，单元ID：{}", unitId);
		return result;
	}

	/**
	 * 根据楼栋ID获取详细地址
	 * 
	 * @param buildingId 楼栋ID
	 * @return 详细地址信息
	 * @throws Exception
	 */
	@GetMapping("/region/address/building/{buildingId}")
	public AddressInfoDTO getAddressByBuildingId(@PathVariable("buildingId") Long buildingId) throws Exception {
		AddressInfoDTO result = addressService.getAddressByBuildingId(buildingId);
		log.info("根据楼栋ID获取详细地址成功，楼栋ID：{}", buildingId);
		return result;
	}

	/**
	 * 根据社区ID获取详细地址
	 * 
	 * @param communityId 社区ID
	 * @return 详细地址信息
	 * @throws Exception
	 */
	@GetMapping("/region/address/community/{communityId}")
	public AddressInfoDTO getAddressByCommunityId(@PathVariable("communityId") Long communityId) throws Exception {
		AddressInfoDTO result = addressService.getAddressByCommunityId(communityId);
		log.info("根据社区ID获取详细地址成功，社区ID：{}", communityId);
		return result;
	}

	/**
	 * 批量根据房户ID获取详细地址
	 * 
	 * @param householdIds 房户ID列表
	 * @return 详细地址信息列表
	 * @throws Exception
	 */
	@PostMapping("/region/address/households/batch")
	public List<AddressInfoDTO> getAddressesByHouseholdIds(@RequestBody List<Long> householdIds) throws Exception {
		List<AddressInfoDTO> result = addressService.getAddressesByHouseholdIds(householdIds);
		log.info("批量根据房户ID获取详细地址成功，房户ID列表：{}", householdIds);
		return result;
	}

	/**
	 * 批量根据单元ID获取详细地址
	 * 
	 * @param unitIds 单元ID列表
	 * @return 详细地址信息列表
	 * @throws Exception
	 */
	@PostMapping("/region/address/units/batch")
	public List<AddressInfoDTO> getAddressesByUnitIds(@RequestBody List<Long> unitIds) throws Exception {
		List<AddressInfoDTO> result = addressService.getAddressesByUnitIds(unitIds);
		log.info("批量根据单元ID获取详细地址成功，单元ID列表：{}", unitIds);
		return result;
	}

	/**
	 * 批量根据楼栋ID获取详细地址
	 * 
	 * @param buildingIds 楼栋ID列表
	 * @return 详细地址信息列表
	 * @throws Exception
	 */
	@PostMapping("/region/address/buildings/batch")
	public List<AddressInfoDTO> getAddressesByBuildingIds(@RequestBody List<Long> buildingIds) throws Exception {
		List<AddressInfoDTO> result = addressService.getAddressesByBuildingIds(buildingIds);
		log.info("批量根据楼栋ID获取详细地址成功，楼栋ID列表：{}", buildingIds);
		return result;
	}

	/**
	 * 批量根据社区ID获取详细地址
	 * 
	 * @param communityIds 社区ID列表
	 * @return 详细地址信息列表
	 * @throws Exception
	 */
	@PostMapping("/region/address/communities/batch")
	public List<AddressInfoDTO> getAddressesByCommunityIds(@RequestBody List<Long> communityIds) throws Exception {
		List<AddressInfoDTO> result = addressService.getAddressesByCommunityIds(communityIds);
		log.info("批量根据社区ID获取详细地址成功，社区ID列表：{}", communityIds);
		return result;
	}

	/**
	 * 查询全部社区集合
	 * 
	 * @return 社区信息列表
	 * @throws Exception 统一异常
	 */
	@GetMapping("/region/communities/all")
	public List<CommunityInfoDTO> getAllCommunities() throws Exception {
		log.info("Feign接口：查询全部社区集合");
		
		try {
			// 调用Feign专用方法，跳过权限验证
			List<CommunityVO> communityVOList = communityService.getAllEnabledCommunitiesForFeign();
			
			if (communityVOList == null || communityVOList.isEmpty()) {
				log.info("未找到任何社区数据");
				return new ArrayList<>();
			}
			
			// 转换为CommunityInfoDTO
			List<CommunityInfoDTO> communityInfoDTOList = communityVOList.stream()
				.map(this::convertToCommunityInfoDTO)
				.filter(Objects::nonNull)
				.collect(Collectors.toList());
			
			log.info("Feign接口：查询全部社区集合成功，社区数量：{}", communityInfoDTOList.size());
			return communityInfoDTOList;
			
		} catch (Exception e) {
			log.error("Feign接口：查询全部社区集合失败", e);
			return new ArrayList<>();
		}
	}

	/**
	 * 创建默认的地址信息对象
	 * 
	 * @return 默认地址信息
	 */
	private AddressInfoDTO createDefaultAddressInfo() {
		AddressInfoDTO addressInfo = new AddressInfoDTO();
		addressInfo.setFullAddress("地址信息暂时不可用");
		addressInfo.setRegionId(0L);
		addressInfo.setRegionName("");
		addressInfo.setCommunityId(0L);
		addressInfo.setCommunityName("");
		addressInfo.setBuildingId(0L);
		addressInfo.setBuildingName("");
		addressInfo.setBuildingNo("");
		addressInfo.setUnitId(0L);
		addressInfo.setUnitName("");
		addressInfo.setUnitNo("");
		addressInfo.setHouseholdId(0L);
		addressInfo.setRoomNumber("");
		return addressInfo;
	}

	/**
	 * 将CommunityVO转换为CommunityInfoDTO
	 * 
	 * @param communityVO CommunityVO对象
	 * @return CommunityInfoDTO对象
	 */
	private CommunityInfoDTO convertToCommunityInfoDTO(CommunityVO communityVO) {
		if (communityVO == null) {
			return null;
		}
		
		CommunityInfoDTO communityInfoDTO = new CommunityInfoDTO();
		communityInfoDTO.setId(communityVO.getId());
		communityInfoDTO.setCommunityName(communityVO.getCommunityName());
		communityInfoDTO.setCommunityCode(communityVO.getCommunityCode());
		communityInfoDTO.setRegionId(communityVO.getRegionId());
		communityInfoDTO.setAddress(communityVO.getAddress());
		communityInfoDTO.setStatus(communityVO.getStatus());
		communityInfoDTO.setCreateTime(communityVO.getCreateTime());
		communityInfoDTO.setUpdateTime(communityVO.getUpdateTime());
		
		return communityInfoDTO;
	}
}